import { modifyPassword } from "@/api/userApi";
import { ResponseCode } from "@/common/Responsetypes";

export interface IUserInfoModifyPaddword {
  email: string;
  password: string;
  code: string;
}

import { computed, reactive } from "vue";

export default function () {
  const userInfo: IUserInfoModifyPaddword = reactive({
    email: "",
    password: "",
    code: "",
  });

  function handleModifyPassword(
    fromEL: { validate: () => Promise<unknown> } | null
  ) {
    fromEL!.validate().then(async () => {
      uni.showLoading();

      const result = await modifyPassword({
        ...userInfo,
      });

      if (result.code === ResponseCode.PASS) {
        uni.hideLoading();
        uni.showToast({
          title: result.msg || "修改成功",
          icon: "none",
          mask: true,
        });

        uni.reLaunch({
          url: "/pages/login/login",
        });
      }
    });
  }

  const canClick = computed(() => {
    return userInfo.email && userInfo.password && userInfo.code ? true : false;
  });

  return {
    handleModifyPassword,
    userInfo,
    canClick,
  };
}
